<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">新增住院电子病历</span>
        <el-button type="primary" size="mini" icon="el-icon-back" @click="handleBack" style="float: right;">返回列表</el-button>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="患者编号" prop="patientSn">
              <el-input v-model="form.patientSn" placeholder="请输入患者编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="就诊编号" prop="patVisitSn">
              <el-input v-model="form.patVisitSn" placeholder="请输入就诊编号" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="病历ID" prop="mrCode">
              <el-input v-model="form.mrCode" placeholder="请输入病历ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住院病历记录编号" prop="inMrSn">
              <el-input v-model="form.inMrSn" placeholder="请输入住院病历记录编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="病历文件名" prop="mrName">
              <el-input v-model="form.mrName" placeholder="请输入病历文件名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文书标题" prop="mrTopic">
              <el-input v-model="form.mrTopic" placeholder="请输入文书标题" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="fileComment">
              <el-input v-model="form.fileComment" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="text-align: center; margin-top: 30px;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="resetForm">重 置</el-button>
        <el-button @click="handleBack">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { addInpatientRecord } from "@/api/ai/inpatientRecord";

export default {
  name: "InpatientRecordAdd",
  data() {
    return {
      form: {
        patientSn: null,
        patVisitSn: null,
        mrCode: null,
        inMrSn: null,
        mrName: null,
        mrTopic: null,
        fileComment: null
      },
      rules: {
        patientSn: [
          { required: true, message: "患者编号不能为空", trigger: "blur" }
        ],
        patVisitSn: [
          { required: true, message: "就诊编号不能为空", trigger: "blur" }
        ],
        mrCode: [
          { required: true, message: "病历ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addInpatientRecord(this.form).then(response => {
            if (response.code === 0) {
              this.$message.success("新增成功");
              this.handleBack();
            } else {
              this.$message.error(response.message || "新增失败");
            }
          }).catch(error => {
            this.$message.error("新增失败：" + error.message);
          });
        }
      });
    },
    /** 重置按钮 */
    resetForm() {
      this.$refs["form"].resetFields();
    },
    /** 返回按钮 */
    handleBack() {
      this.$router.push("/ai/inpatientRecord");
    }
  }
};
</script>